package vn.student.mylib;
import java.util.ArrayList;
import vn.student.R;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ListAssignmentInPlannerAdapter extends ArrayAdapter<Assignment>{
	Activity act;
	public ArrayList<Assignment> list;
	boolean isViewStudentAss = false;
	public ListAssignmentInPlannerAdapter(Activity act,ArrayList<Assignment> list,boolean isViewStudentAss) {
		super(act, R.layout.assignment_row_2,list);
		this.act=act;
		this.list = list;
		this.isViewStudentAss = isViewStudentAss;
		if(list!=null&&list.size()>0){
			((ListView)act.findViewById(R.id.listView)).getLayoutParams().height=list.size()*90+20;
		}
	}
	public View getView(int position, View convertView,ViewGroup parent) {
		 View row=convertView;
		 RowWrapper wrapper=null;
	      
	     if (row==null) {                         
	    	 LayoutInflater inflater=act.getLayoutInflater();
	    	 row=inflater.inflate(R.layout.assignment_row_2, null);
	    	 wrapper=new RowWrapper(row);
	    	 row.setTag(wrapper);
	     }
	     else {
	    	 wrapper=(RowWrapper)row.getTag();
	     }
	     Assignment tmp = list.get(position);
	     wrapper.getAssNameLabel().setText(tmp.name);
	     wrapper.getClassNameLabel().setText(tmp.className);
	     wrapper.getAssDesLabel().setText(tmp.des);
	     wrapper.getAssDueDateLabel().setText(tmp.dueDate);
	     wrapper.getPriIcon().setImageResource(AssignmentPriorityIconId.getIconId(tmp.priority));
	     if(tmp.done)
	     	wrapper.getIsDoneIcon().setImageResource(R.drawable.ass_done);
	     else
	    	 wrapper.getIsDoneIcon().setImageResource(R.drawable.ass_undone);
	     if(list.size()==1){
    		 row.setBackgroundResource(R.drawable.class_row_all);
    	 }
    	 else{
    		 if(position==(list.size()-1))
    			 row.setBackgroundResource(R.drawable.class_row_bottom);
    		 else 
    			 if(position==0)
    				 row.setBackgroundResource(R.drawable.class_row_top);
    			 else
    				 row.setBackgroundResource(R.drawable.class_row_none);
    	 }
	     return(row);
	}
	public ArrayList<Assignment> getClassInfoList(){
		return list;
	}
	public void setClassInfoList(ArrayList<Assignment> list){
		this.list = list;
	}
	
	private class RowWrapper {
		View base;
		TextView classNameLabel = null;
		TextView assNameLabel = null;
		TextView assDesLabel=null;
		TextView assDueDateLabel=null;
		ImageView assIsDone=null;
		ImageView assPri=null;
		RowWrapper(View base) {
			this.base=base;
		}
		TextView getClassNameLabel() {
			if (classNameLabel==null) {
				classNameLabel=(TextView)base.findViewById(R.id.classNameLabel);
			}
		  return(classNameLabel);
		}
		TextView getAssNameLabel() {
			if (assNameLabel==null) {
				assNameLabel=(TextView)base.findViewById(R.id.assNameLabel);
			}
		  return(assNameLabel);
		}
		TextView getAssDesLabel() {
			if (assDesLabel==null) {
				assDesLabel=(TextView)base.findViewById(R.id.assDesLabel);
			}
		  return(assDesLabel);
		}
		TextView getAssDueDateLabel() {
			if (assDueDateLabel==null) {
				assDueDateLabel=(TextView)base.findViewById(R.id.assDueDateLabel);
			}
		  return(assDueDateLabel);
		}
		ImageView getIsDoneIcon() {
			if (assIsDone==null) {
				assIsDone=(ImageView)base.findViewById(R.id.isDoneImg);
			}
			return(assIsDone);
		}
		ImageView getPriIcon() {
			if (assPri==null) {
				assPri=(ImageView)base.findViewById(R.id.priIcon);
			}
			return(assPri);
		}
	}
}

